package com.css.fxfzfxqh.modules.lifelineEngineering.service.impl;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.JSON;
import com.css.fxfzfxqh.common.rest.JcsjRestManager;
import com.css.fxfzfxqh.common.rest.model.param.AnalysisParam;
import com.css.fxfzfxqh.common.rest.model.vo.ysx.*;
import com.css.fxfzfxqh.constants.EvaluationObjectEnum;
import com.css.fxfzfxqh.modules.lifelineEngineering.constants.FunctionalFailureConstants;
import com.css.fxfzfxqh.modules.lifelineEngineering.constants.TypeConstants;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.EvaluationResults;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.SmxVectorEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.EstimateRepository;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.TaskManagementRepository;
import com.css.fxfzfxqh.modules.lifelineEngineering.service.EstimateService;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.CurrentTaskVo;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.VectorVo;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.smx.*;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.smx.influenceField.*;
import com.css.fxfzfxqh.modules.seismicHazardData.entity.SettingInfluenceFliedEntity;
import com.css.fxfzfxqh.modules.seismicHazardData.repository.SeismicImpactFieldRepository;
import com.css.fxfzfxqh.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.geotools.data.FeatureWriter;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
@Slf4j
public class EstimateServiceImpl implements EstimateService {

    @Autowired
    private EstimateRepository estimateRepository;

    @Autowired
    private TaskManagementRepository taskManagementRepository;

    @Autowired
    private JcsjRestManager jcsjRestManager;

    @Autowired
    private SeismicImpactFieldRepository seismicImpactFieldRepository;

    @Override
    public CurrentTaskVo getCurrentTask() {
        //当前任务
        String userId = PlatformSessionUtils.getUserId();
        LifelineTaskEntity taskEntity = taskManagementRepository.getCurrentTask(userId);
        if (PlatformObjectUtils.isNotEmpty(taskEntity)) {
            List<EvaluationResults> list = estimateRepository.getListByTaskId(taskEntity.getId());
            List<EvaluationResults> lineList = estimateRepository.getLineListByTaskId(taskEntity.getId());
            list.addAll(lineList);
            return entityToVo(taskEntity, list);
        }
        return null;
    }

    private CurrentTaskVo entityToVo(LifelineTaskEntity entity, List<EvaluationResults> list) {
        return CurrentTaskVo.builder()
                .id(entity.getId())
                .code(entity.getCode())
                .name(entity.getName())
                .evaluationObject(EvaluationObjectEnum.getByCode(entity.getEvaluationObject()))
                .estimate(entity.getEvaluationObject())
                .inputMethod(entity.getInputMethod())
                .intensity(entity.getIntensity())
                .list(list)
                .build();
    }

    @Override
    public void exportExcel(String taskId, Integer type, HttpServletResponse response) throws Exception {
        LifelineTaskEntity entity = taskManagementRepository.getEntityById(taskId);
        Integer inputMethod = entity.getInputMethod();
        List<EvaluationResults> list = estimateRepository.getListByTaskIdAndType(taskId, type);
        List<EvaluationResults> lineList = estimateRepository.getLineListByTaskIdAndType(taskId, type);
        list.addAll(lineList);
        Object dataList = dataProcessing(type, list, inputMethod);
        if (type == 1 || type == 15) {
            String name = "地下管网";
            if (type == 15) {
                name = "供气管网";
            }
            if (inputMethod == 1) {
                List<UpNetworkVo> upNetworkList = (List<UpNetworkVo>) dataList;
                FileUtil.exportExcel(upNetworkList, name + "评估结果", "sheet1", UpNetworkVo.class, name, response);
            } else {
                List<UpNetworkIfVo> upNetworkList = (List<UpNetworkIfVo>) dataList;
                FileUtil.exportExcel(upNetworkList, name + "评估结果", "sheet1", UpNetworkIfVo.class, name, response);
            }
        } else if (type == 2) {
            if (inputMethod == 1) {
                List<PoolVo> poolVoList = (List<PoolVo>) dataList;
                FileUtil.exportExcel(poolVoList, "水厂水池评估结果", "sheet1", PoolVo.class, "水厂水池", response);
            } else {
                List<PoolIfVo> poolVoList = (List<PoolIfVo>) dataList;
                FileUtil.exportExcel(poolVoList, "水厂水池评估结果", "sheet1", PoolIfVo.class, "水厂水池", response);
            }
        } else if (type == 3) {
            if (inputMethod == 1) {
                List<GasVo> poolVoList = (List<GasVo>) dataList;
                FileUtil.exportExcel(poolVoList, "燃气-储气罐评估结果", "sheet1", GasVo.class, "燃气-储气罐", response);
            } else {
                List<GasIfVo> poolVoList = (List<GasIfVo>) dataList;
                FileUtil.exportExcel(poolVoList, "燃气-储气罐评估结果", "sheet1", GasIfVo.class, "燃气-储气罐", response);
            }
        } else if (type == 4) {
            if (inputMethod == 1) {
                List<BridgeVo> bridgeList = (List<BridgeVo>) dataList;
                FileUtil.exportExcel(bridgeList, "桥梁评估结果", "sheet1", BridgeVo.class, "桥梁", response);
            } else {
                List<BridgeIfVo> bridgeList = (List<BridgeIfVo>) dataList;
                FileUtil.exportExcel(bridgeList, "桥梁评估结果", "sheet1", BridgeIfVo.class, "桥梁", response);
            }
        } else if (type == 5) {
            if (inputMethod == 1) {
                List<RoadVo> roadList = (List<RoadVo>) dataList;
                FileUtil.exportExcel(roadList, "道路评估结果", "sheet1", RoadVo.class, "道路", response);
            } else {
                List<RoadIfVo> roadList = (List<RoadIfVo>) dataList;
                FileUtil.exportExcel(roadList, "道路评估结果", "sheet1", RoadIfVo.class, "道路", response);
            }
        } else if (type == 6) {
            if (inputMethod == 1) {
                List<TransformerSubstationVo> tsList = (List<TransformerSubstationVo>) dataList;
                FileUtil.exportExcel(tsList, "变电站评估结果", "sheet1", TransformerSubstationVo.class, "变电站", response);
            } else {
                List<TransformerSubstationIfVo> tsList = (List<TransformerSubstationIfVo>) dataList;
                FileUtil.exportExcel(tsList, "变电站评估结果", "sheet1", TransformerSubstationIfVo.class, "变电站", response);
            }
        } else if (type == 8) {
            if (inputMethod == 1) {
                List<CablingVo> cablingList = (List<CablingVo>) dataList;
                FileUtil.exportExcel(cablingList, "电缆线路评估结果", "sheet1", CablingVo.class, "电缆线路", response);
            } else {
                List<CablingIfVo> cablingList = (List<CablingIfVo>) dataList;
                FileUtil.exportExcel(cablingList, "电缆线路评估结果", "sheet1", CablingIfVo.class, "电缆线路", response);
            }
        } else if (type == 9) {
            if (inputMethod == 1) {
                List<DevicesVo> devicesList = (List<DevicesVo>) dataList;
                FileUtil.exportExcel(devicesList, "通信室内设备评估结果", "sheet1", DevicesVo.class, "通信室内设备", response);
            } else {
                List<DevicesIfVo> devicesList = (List<DevicesIfVo>) dataList;
                FileUtil.exportExcel(devicesList, "通信室内设备评估结果", "sheet1", DevicesIfVo.class, "通信室内设备", response);
            }
        } else if (type == 11) {
            if (inputMethod == 1) {
                List<PumpRoomVo> pumpRoomList = (List<PumpRoomVo>) dataList;
                FileUtil.exportExcel(pumpRoomList, "水厂泵房评估结果", "sheet1", PumpRoomVo.class, "水厂泵房", response);
            } else {
                List<PumpRoomIfVo> pumpRoomList = (List<PumpRoomIfVo>) dataList;
                FileUtil.exportExcel(pumpRoomList, "水厂泵房评估结果", "sheet1", PumpRoomIfVo.class, "水厂泵房", response);
            }
        } else if (type == 12) {
            if (inputMethod == 1) {
                List<PowerhouseVo> powerhouseList = (List<PowerhouseVo>) dataList;
                FileUtil.exportExcel(powerhouseList, "重要发电厂房评估结果", "sheet1", PowerhouseVo.class, "重要发电厂房", response);
            } else {
                List<PowerhouseIfVo> powerhouseList = (List<PowerhouseIfVo>) dataList;
                FileUtil.exportExcel(powerhouseList, "重要发电厂房评估结果", "sheet1", PowerhouseIfVo.class, "重要发电厂房", response);
            }
        } else if (type == 13) {
            if (inputMethod == 1) {
                List<BaseStationVo> baseStationList = (List<BaseStationVo>) dataList;
                FileUtil.exportExcel(baseStationList, "通信基站评估结果", "sheet1", BaseStationVo.class, "通信基站", response);
            } else {
                List<BaseStationIfVo> baseStationList = (List<BaseStationIfVo>) dataList;
                FileUtil.exportExcel(baseStationList, "通信基站评估结果", "sheet1", BaseStationIfVo.class, "通信基站", response);
            }
        } else if (type == 14) {
            if (inputMethod == 1) {
                List<GasStoreVo> gasList = (List<GasStoreVo>) dataList;
                FileUtil.exportExcel(gasList, "燃气-门站/场站评估结果", "sheet1", GasStoreVo.class, "燃气-门站/场站", response);
            } else {
                List<GasStoreIfVo> gasList = (List<GasStoreIfVo>) dataList;
                FileUtil.exportExcel(gasList, "燃气-门站/场站评估结果", "sheet1", GasStoreIfVo.class, "燃气-门站/场站", response);
            }
        }
    }

    @Value("${image.localDir}")
    private String localDir;

    @Override
    public void exportShp(String taskId, Integer type, HttpServletResponse response) throws Exception {
        //创建shp文件
        String name = TypeConstants.getConstant(type);
        String path = localDir + File.separator + name + ".shp";
        File file = new File(path);
        Map params = new HashMap();
        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
        ShapefileDataStoreFactory sf = new ShapefileDataStoreFactory();
        ShapefileDataStore ds = (ShapefileDataStore) sf.createDataStore(file.toURI().toURL());
        ds.setCharset(Charset.forName("GBK"));
        SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
        tb.setCRS(CRS.decode("EPSG:4490"));
        tb.setName("shapefile");

        //数据
        LifelineTaskEntity entity = taskManagementRepository.getEntityById(taskId);
        List<EvaluationResults> list = estimateRepository.getListByTaskIdAndType(taskId, type);
        List<EvaluationResults> lineList = estimateRepository.getLineListByTaskIdAndType(taskId, type);
        list.addAll(lineList);

        if (type == 1 || type == 15) {
            //地下管网
            exportUpNetwork(list, tb, ds, entity.getInputMethod());
        } else if (type == 2) {
            exportWaterPlantPool(list, tb, ds, entity.getInputMethod());
        } else if (type == 3) {
            exportGas(list, tb, ds, entity.getInputMethod());
        } else if (type == 4) {
            exportBridge(list, tb, ds, entity.getInputMethod());
        } else if (type == 5) {
            exportRoad(list, tb, ds, entity.getInputMethod());
        } else if (type == 6) {
            exportTransformerSubstation(list, tb, ds, entity.getInputMethod());
        } else if (type == 8) {
            exportCabling(list, tb, ds, entity.getInputMethod());
        } else if (type == 9) {
            exportEquipment(list, tb, ds, entity.getInputMethod());
        } else if (type == 11) {
            exportPumpRoom(list, tb, ds, entity.getInputMethod());
        } else if (type == 12) {
            exportPowerhouse(list, tb, ds, entity.getInputMethod());
        } else if (type == 13) {
            exportBaseStation(list, tb, ds, entity.getInputMethod());
        } else if (type == 14) {
            exportGasStore(list, tb, ds, entity.getInputMethod());
        }
        try {
            String zipPath = localDir + File.separator + name + ".zip";
            File zipFile = new File(zipPath);
            InputStream input = null;
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            // zip的名称为
            zipOut.setComment(name);
            String[] shpFiles = new String[]{
                    localDir + File.separator + name + ".dbf",
                    localDir + File.separator + name + ".prj",
                    localDir + File.separator + name + ".shp",
                    localDir + File.separator + name + ".shx",
                    localDir + File.separator + name + ".fix"
            };

            for (int i = 0; i < shpFiles.length; i++) {
                File file1 = new File(shpFiles[i]);
                input = new FileInputStream(file1);
                zipOut.putNextEntry(new ZipEntry(file1.getName()));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                input.close();
            }
            zipOut.close();
            //拼接下载默认名称并转为ISO-8859-1格式
            String fileName = new String((name + ".zip").getBytes(), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attchment;filename=" + fileName);
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Method", "POST,GET");
            //该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
            ServletOutputStream outputStream = response.getOutputStream();
            FileInputStream inputStream = new FileInputStream(zipPath);
            // copy方法为文件复制,在这里直接实现了下载效果
            IOUtils.copy(inputStream, outputStream);
            // 关闭输入流
            inputStream.close();
            File file1 = new File(localDir);
            File[] files = file1.listFiles();
            for (File file2 : files) {
                if (file2.getName().contains(name)) {
                    file2.delete();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void exportGasStore(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("建筑物序号", String.class);
        tb.add("燃气-门店/场站名称", String.class);
        tb.add("结构类型", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            if (inputMethod == 1) {
                feature.setAttribute(4, entity.getDisasterRate6State());
                feature.setAttribute(5, entity.getDisasterRate7State());
                feature.setAttribute(6, entity.getDisasterRate8State());
                feature.setAttribute(7, entity.getDisasterRate9State());
                feature.setAttribute(8, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(6, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportBaseStation(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("建筑物序号", String.class);
        tb.add("基站名称", String.class);
        tb.add("结构类型", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getLongitude());
            feature.setAttribute(5, entity.getLatitude());
            feature.setAttribute(6, entity.getExtends4());
            feature.setAttribute(7, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportPowerhouse(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("电厂名称", String.class);
        tb.add("结构类型", String.class);
        tb.add("位置", String.class);
        tb.add("经度", String.class);
        tb.add("纬度", String.class);
        tb.add("发电类型", String.class);
        tb.add("发电厂总造价", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getLongitude());
            feature.setAttribute(5, entity.getLatitude());
            feature.setAttribute(6, entity.getExtends4());
            feature.setAttribute(7, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportPumpRoom(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("名称", String.class);
        tb.add("经度", String.class);
        tb.add("纬度", String.class);
        tb.add("建筑物编码", String.class);
        tb.add("建筑物名称", String.class);
        tb.add("结构类型", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getLongitude());
            feature.setAttribute(3, entity.getLatitude());
            feature.setAttribute(4, entity.getExtends2());
            feature.setAttribute(5, entity.getExtends3());
            feature.setAttribute(6, entity.getExtends4());
            if (inputMethod == 1) {
                feature.setAttribute(7, entity.getDisasterRate6State());
                feature.setAttribute(8, entity.getDisasterRate7State());
                feature.setAttribute(9, entity.getDisasterRate8State());
                feature.setAttribute(10, entity.getDisasterRate9State());
                feature.setAttribute(11, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(7, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportEquipment(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("所属建筑物名称", String.class);
        tb.add("所属建筑物地址位置", String.class);
        tb.add("建设年代", String.class);
        tb.add("设防情况", String.class);
        tb.add("设备类别", String.class);
        tb.add("场地分类", String.class);
        tb.add("设备名称", String.class);
        tb.add("型号", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getExtends4());
            feature.setAttribute(5, entity.getExtends5());
            feature.setAttribute(6, entity.getExtends5());
            feature.setAttribute(7, entity.getExtends5());
            feature.setAttribute(8, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(9, entity.getDisasterRate6State());
                feature.setAttribute(10, entity.getDisasterRate7State());
                feature.setAttribute(11, entity.getDisasterRate8State());
                feature.setAttribute(12, entity.getDisasterRate9State());
                feature.setAttribute(13, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(9, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportCabling(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("评价单元编码", String.class);
        tb.add("评价单元名称", String.class);
        tb.add("评价单元设防烈度", String.class);
        tb.add("线路总长度", String.class);
        tb.add("所有电缆线路总体造价", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getExtends4());
            feature.setAttribute(5, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(6, entity.getDisasterRate6State());
                feature.setAttribute(7, entity.getDisasterRate7State());
                feature.setAttribute(8, entity.getDisasterRate8State());
                feature.setAttribute(9, entity.getDisasterRate9State());
                feature.setAttribute(10, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(6, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportTransformerSubstation(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("变电站编码", String.class);
        tb.add("变电站名称", String.class);
        tb.add("变电站位置", String.class);
        tb.add("经度", String.class);
        tb.add("纬度", String.class);
        tb.add("变电站类型", String.class);
        tb.add("变电站电压等级", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getLongitude());
            feature.setAttribute(5, entity.getLatitude());
            feature.setAttribute(6, entity.getExtends4());
            feature.setAttribute(7, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportRoad(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("编码", String.class);
        tb.add("道路名称", String.class);
        tb.add("道路等级", String.class);
        tb.add("基本烈度", String.class);
        tb.add("路基土", String.class);
        tb.add("场地类别", String.class);
        tb.add("设防情况", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", Polygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getExtends4());
            feature.setAttribute(5, entity.getExtends5());
            feature.setAttribute(6, entity.getExtends6());
            feature.setAttribute(7, entity.getExtends7());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportBridge(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("编码", String.class);
        tb.add("桥梁名称", String.class);
        tb.add("经度", String.class);
        tb.add("纬度", String.class);
        tb.add("设防情况", String.class);
        tb.add("抗震设防烈度", String.class);
        tb.add("建造年份", String.class);
        tb.add("桥梁结构类型", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        if (list.get(0).getGeom().contains("POINT")) {
            tb.add("the_geom", MultiPolygon.class);
        } else {
            tb.add("the_geom", Polygon.class);
        }
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getLongitude());
            feature.setAttribute(4, entity.getLatitude());
            feature.setAttribute(5, entity.getExtends3());
            feature.setAttribute(6, entity.getExtends4());
            feature.setAttribute(7, entity.getExtends5());
            feature.setAttribute(8, entity.getExtends6());
            if (inputMethod == 1) {
                feature.setAttribute(9, entity.getDisasterRate6State());
                feature.setAttribute(10, entity.getDisasterRate7State());
                feature.setAttribute(11, entity.getDisasterRate8State());
                feature.setAttribute(12, entity.getDisasterRate9State());
                feature.setAttribute(13, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(9, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportGas(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("储气罐编码", String.class);
        tb.add("储气罐名称", String.class);
        tb.add("经度", String.class);
        tb.add("纬度", String.class);
        tb.add("型号", String.class);
        tb.add("公称容积", String.class);
        tb.add("工作压力", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getLongitude());
            feature.setAttribute(4, entity.getLatitude());
            feature.setAttribute(5, entity.getExtends3());
            feature.setAttribute(6, entity.getExtends4());
            feature.setAttribute(7, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportWaterPlantPool(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("名称", String.class);
        tb.add("经度", String.class);
        tb.add("建筑物编码", String.class);
        tb.add("建筑物名称", String.class);
        tb.add("结构类型", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", MultiPolygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getExtends4());
            feature.setAttribute(5, entity.getExtends5());
            feature.setAttribute(6, entity.getExtends6());
            feature.setAttribute(7, entity.getExtends7());
            if (inputMethod == 1) {
                feature.setAttribute(8, entity.getDisasterRate6State());
                feature.setAttribute(9, entity.getDisasterRate7State());
                feature.setAttribute(10, entity.getDisasterRate8State());
                feature.setAttribute(11, entity.getDisasterRate9State());
                feature.setAttribute(12, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(8, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    private void exportUpNetwork(List<EvaluationResults> list, SimpleFeatureTypeBuilder tb, ShapefileDataStore ds, Integer inputMethod) throws Exception {
        tb.add("管道编码", String.class);
        tb.add("管道名称", String.class);
        tb.add("管道类型", String.class);
        tb.add("起点编号", String.class);
        tb.add("终点编号", String.class);
        if (inputMethod == 1) {
            tb.add("VI破坏状态", String.class);
            tb.add("VII破坏状态", String.class);
            tb.add("VIII破坏状态", String.class);
            tb.add("IX破坏状态", String.class);
            tb.add("X破坏状态", String.class);
        } else {
            tb.add("破坏状态", String.class);
        }
        tb.add("the_geom", Polygon.class);
        ds.createSchema(tb.buildFeatureType());
        FeatureWriter featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
        for (EvaluationResults entity : list) {
            SimpleFeature feature = (SimpleFeature) featureWriter.next();
            feature.setAttribute(1, entity.getExtends1());
            feature.setAttribute(2, entity.getExtends2());
            feature.setAttribute(3, entity.getExtends3());
            feature.setAttribute(4, entity.getExtends4());
            feature.setAttribute(5, entity.getExtends5());
            if (inputMethod == 1) {
                feature.setAttribute(6, entity.getDisasterRate6State());
                feature.setAttribute(7, entity.getDisasterRate7State());
                feature.setAttribute(8, entity.getDisasterRate8State());
                feature.setAttribute(9, entity.getDisasterRate9State());
                feature.setAttribute(10, entity.getDisasterRate10State());
            } else {
                feature.setAttribute(6, entity.getDisasterRateState());
            }
        }
        featureWriter.write();
        featureWriter.close();
        ds.dispose();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HashMap<Integer, Object> damageLevelAnalysis(List<AnalysisParam> listParam) throws Exception {
        String userId = PlatformSessionUtils.getUserId();
        LifelineTaskEntity taskEntity = taskManagementRepository.getCurrentTask(userId);
        HashMap<Integer, Object> map = new HashMap<>();
        for (AnalysisParam param : listParam) {
            AnalysisParam analysisParam = new AnalysisParam();
            analysisParam.setType(param.getType());
            analysisParam.setVersionId(param.getVersionId());
            //根据版本号查数据
            List<EvaluationResults> data = jcsjRestManager.getAnalysisByVersionId(analysisParam).getData();
            if (data != null && data.size() > 0) {
                //版本号有数据
            } else {
                //删除数据
                estimateRepository.deleteByTaskIdAndType(taskEntity.getId(), param.getType());
                estimateRepository.deleteLineByTaskIdAndType(taskEntity.getId(), param.getType());
                continue;
            }
            if (PlatformObjectUtils.isNotEmpty(param.getRang()) && PlatformObjectUtils.isNotEmpty(data)) {
                for (EvaluationResults entity : data) {
                    DestructiveStateVo destructiveState = jcsjRestManager.getByTypeAndLifelineIdAndIntensity(entity.getId(), entity.getType(), param.getRang());
                    if (PlatformObjectUtils.isNotEmpty(destructiveState)) {
                        if (PlatformObjectUtils.isNotEmpty(destructiveState.getDisasterRate6State())) {
                            entity.setDisasterRate6State(destructiveState.getDisasterRate6State());
                        }
                        if (PlatformObjectUtils.isNotEmpty(destructiveState.getDisasterRate7State())) {
                            entity.setDisasterRate7State(destructiveState.getDisasterRate7State());
                        }
                        if (PlatformObjectUtils.isNotEmpty(destructiveState.getDisasterRate8State())) {
                            entity.setDisasterRate8State(destructiveState.getDisasterRate8State());
                        }
                        if (PlatformObjectUtils.isNotEmpty(destructiveState.getDisasterRate9State())) {
                            entity.setDisasterRate9State(destructiveState.getDisasterRate9State());
                        }
                        if (PlatformObjectUtils.isNotEmpty(destructiveState.getDisasterRate10State())) {
                            entity.setDisasterRate10State(destructiveState.getDisasterRate10State());
                        }
                    }
                }
            }

            if (PlatformObjectUtils.isNotEmpty(data)) {
                data.forEach(it -> {
                    if (PlatformObjectUtils.isNotEmpty(it.getDisasterRate6State())) {
                        it.setFunctionalFailure6(FunctionalFailureConstants.getConstant(it.getDisasterRate6State()));
                        it.setFunctionalFailure6Correct(FunctionalFailureConstants.getConstant(it.getDisasterRate6State()));
                    }
                    if (PlatformObjectUtils.isNotEmpty(it.getDisasterRate7State())) {
                        it.setFunctionalFailure7(FunctionalFailureConstants.getConstant(it.getDisasterRate7State()));
                        it.setFunctionalFailure7Correct(FunctionalFailureConstants.getConstant(it.getDisasterRate7State()));
                    }
                    if (PlatformObjectUtils.isNotEmpty(it.getDisasterRate8State())) {
                        it.setFunctionalFailure8(FunctionalFailureConstants.getConstant(it.getDisasterRate8State()));
                        it.setFunctionalFailure8Correct(FunctionalFailureConstants.getConstant(it.getDisasterRate8State()));
                    }
                    if (PlatformObjectUtils.isNotEmpty(it.getDisasterRate9State())) {
                        it.setFunctionalFailure9(FunctionalFailureConstants.getConstant(it.getDisasterRate9State()));
                        it.setFunctionalFailure9Correct(FunctionalFailureConstants.getConstant(it.getDisasterRate9State()));
                    }
                    if (PlatformObjectUtils.isNotEmpty(it.getDisasterRate10State())) {
                        it.setFunctionalFailure10(FunctionalFailureConstants.getConstant(it.getDisasterRate10State()));
                        it.setFunctionalFailure10Correct(FunctionalFailureConstants.getConstant(it.getDisasterRate10State()));
                    }
                });
            }

            if (taskEntity.getInputMethod() == 2) {
                ArrayList<EvaluationResults> list = new ArrayList<>();
                List<SettingInfluenceFliedEntity> influenceFliedList = seismicImpactFieldRepository.getInfluencefliedById(taskEntity.getInfluenceField());
                for (SettingInfluenceFliedEntity entity : influenceFliedList) {
                    List<String> centerPoint = estimateRepository.getCenterPoint(entity.getGeom());
                    for (EvaluationResults resultEntity : data) {
                        String dzIntensity = entity.getDzIntensity();
                        if (centerPoint.get(0).equals(resultEntity.getLongitude()) && centerPoint.get(1).equals(resultEntity.getLatitude())) {
                            String destructiveState = "";
                            switch (dzIntensity) {
                                case "6":
                                    destructiveState = resultEntity.getDisasterRate6State();
                                    break;
                                case "7":
                                    destructiveState = resultEntity.getDisasterRate7State();
                                    break;
                                case "8":
                                    destructiveState = resultEntity.getDisasterRate8State();
                                    break;
                                case "9":
                                    destructiveState = resultEntity.getDisasterRate9State();
                                    break;
                                case "10":
                                    destructiveState = resultEntity.getDisasterRate10State();
                            }
                            if (PlatformObjectUtils.isNotEmpty(destructiveState)) {
                                resultEntity.setDisasterRateState(destructiveState.equals("毁坏") ? "倒塌" : resultEntity.getDisasterRateState());
                                resultEntity.setDisasterRateStateCorrection(destructiveState.equals("毁坏") ? "倒塌" : resultEntity.getDisasterRateState());
                                resultEntity.setFunctionalFailure(FunctionalFailureConstants.getConstant(destructiveState));
                            }
                            list.add(resultEntity);
                        }
                    }
                }
                data = list;
            }
            ArrayList<EvaluationResultsVo> resultList = new ArrayList<>();

            if (estimateRepository.getByTaskIdAndType(taskEntity.getId(), param.getType()) != 0 || estimateRepository.getLineByTaskIdAndType(taskEntity.getId(), param.getType()) != 0) {
                estimateRepository.deleteByTaskIdAndType(taskEntity.getId(), param.getType());
                estimateRepository.deleteLineByTaskIdAndType(taskEntity.getId(), param.getType());
            }

            if (PlatformObjectUtils.isNotEmpty(data)) {
                for (EvaluationResults entity : data) {
                    EvaluationResultsVo vo = new EvaluationResultsVo();
                    entity.setId(UUIDGenerator.getUUID());
                    entity.setType(param.getType());
                    entity.setDelFlag("0");
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRateState())) {
                        entity.setDisasterRateState(entity.getDisasterRateState().equals("毁坏") ? "倒塌" : entity.getDisasterRateState());
                        entity.setDisasterRateStateCorrection(entity.getDisasterRateState());
                    }
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRate6State())) {
                        entity.setDisasterRate6State(entity.getDisasterRate6State().equals("毁坏") ? "倒塌" : entity.getDisasterRate6State());
                        entity.setDisasterRate6StateCorrection(entity.getDisasterRate6State());
                    }
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRate7State())) {
                        entity.setDisasterRate7State(entity.getDisasterRate7State().equals("毁坏") ? "倒塌" : entity.getDisasterRate7State());
                        entity.setDisasterRate7StateCorrection(entity.getDisasterRate7State());
                    }
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRate8State())) {
                        entity.setDisasterRate8State(entity.getDisasterRate8State().equals("毁坏") ? "倒塌" : entity.getDisasterRate8State());
                        entity.setDisasterRate8StateCorrection(entity.getDisasterRate8State());
                    }
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRate9State())) {
                        entity.setDisasterRate9State(entity.getDisasterRate9State().equals("毁坏") ? "倒塌" : entity.getDisasterRate9State());
                        entity.setDisasterRate9StateCorrection(entity.getDisasterRate9State());
                    }
                    if (PlatformObjectUtils.isNotEmpty(entity.getDisasterRate10State())) {
                        entity.setDisasterRate10State(entity.getDisasterRate10State().equals("毁坏") ? "倒塌" : entity.getDisasterRate10State());
                        entity.setDisasterRate10StateCorrection(entity.getDisasterRate10State());
                    }
                    entity.setDataSourceId(param.getVersionId());
                    entity.setTaskId(taskEntity.getId());
                    entity.setRang(param.getRang());
                    entity.setVersionCode(param.getVersionCode());
                    BeanUtils.copyProperties(entity, vo);
                    resultList.add(vo);
                    if (entity.getGeom().contains("POINT")) {
                        //点
                        estimateRepository.save(entity);
                    } else {
                        //线
                        estimateRepository.saveLine(entity);
                    }
                }
            }
            if (PlatformObjectUtils.isNotEmpty(resultList)) {
                map.put(param.getType(), resultList);
            }
        }
        return map;
    }

    /**
     * 数据处理  转不同类型的vo
     */
    private Object dataProcessing(Integer type, List<EvaluationResults> list, Integer inputMethod) {
        Object resultList = null;
        if (type == 1 || type == 15) {
            ArrayList<UpNetworkVo> upNetworkList = new ArrayList<UpNetworkVo>();
            list.forEach(it -> {
                UpNetworkVo entity = new UpNetworkVo();
                entity.setUpId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setType(it.getExtends3());
                entity.setStartId(it.getExtends4());
                entity.setEndId(it.getExtends5());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                upNetworkList.add(entity);
            });
            resultList = upNetworkList;
        } else if (type == 2) {
            ArrayList<PoolVo> poolList = new ArrayList<>();
            list.forEach(it -> {
                PoolVo entity = new PoolVo();
                entity.setPoolId(it.getExtends1());
                entity.setStructuralStyle(it.getExtends2());
                entity.setStructuralMaterials(it.getExtends3());
                entity.setWallThickness(it.getExtends4());
                entity.setShape(it.getExtends5());
                entity.setRadius(it.getExtends6());
                entity.setShortSide(it.getExtends7());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                poolList.add(entity);
            });
            resultList = poolList;
        } else if (type == 3) {
            ArrayList<GasVo> gasList = new ArrayList<>();
            list.forEach(it -> {
                GasVo entity = new GasVo();
                entity.setGasId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setLongitude(it.getLongitude());
                entity.setLatitude(it.getLatitude());
                entity.setModel(it.getExtends3());
                entity.setNominalVolume(it.getExtends4());
                entity.setWorkingPressure(it.getExtends5());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                gasList.add(entity);
            });
            resultList = gasList;
        } else if (type == 4) {
            ArrayList<BridgeVo> bridgeList = new ArrayList<>();
            list.forEach(it -> {
                BridgeVo entity = new BridgeVo();
                entity.setBridgeId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setLongitude(it.getLongitude());
                entity.setLatitude(it.getLatitude());
                entity.setFortification(it.getExtends3());
                entity.setIntensity(it.getExtends4());
                entity.setYear(it.getExtends5());
                entity.setStructure(it.getExtends6());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                bridgeList.add(entity);
            });
            resultList = bridgeList;
        } else if (type == 5) {
            ArrayList<RoadVo> roadList = new ArrayList<>();
            list.forEach(it -> {
                RoadVo entity = new RoadVo();
                entity.setRoadId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setGrade(it.getExtends3());
                entity.setFortification(it.getExtends4());
                entity.setRoadbedSoil(it.getExtends5());
                entity.setSiteSoil(it.getExtends6());
                entity.setFortification(it.getExtends7());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                roadList.add(entity);
            });
            resultList = roadList;
        } else if (type == 6) {
            ArrayList<TransformerSubstationVo> tsList = new ArrayList<>();
            list.forEach(it -> {
                TransformerSubstationVo entity = new TransformerSubstationVo();
                entity.setTsId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setPosition(it.getExtends3());
                entity.setLongitude(it.getLongitude());
                entity.setLatitude(it.getLatitude());
                entity.setType(it.getExtends4());
                entity.setGrade(it.getExtends5());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                tsList.add(entity);
            });
            resultList = tsList;
        } else if (type == 8) {
            ArrayList<CablingVo> cablingList = new ArrayList<>();
            list.forEach(it -> {
                CablingVo entity = new CablingVo();
                entity.setCablingId(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setIntensity(it.getExtends3());
                entity.setLength(it.getExtends4());
                entity.setConstruction(it.getExtends5());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                cablingList.add(entity);
            });
            resultList = cablingList;
        } else if (type == 9) {
            ArrayList<DevicesVo> deviceList = new ArrayList<>();
            list.forEach(it -> {
                DevicesVo entity = new DevicesVo();
                entity.setName(it.getExtends1());
                entity.setPosition(it.getExtends2());
                entity.setYear(it.getExtends3());
                entity.setFortification(it.getExtends4());
                entity.setCategory(it.getExtends5());
                entity.setType(it.getExtends6());
                entity.setDeviceName(it.getExtends7());
                entity.setModel(it.getExtends8());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                deviceList.add(entity);
            });
            resultList = deviceList;
        } else if (type == 11) {
            ArrayList<PumpRoomVo> roomList = new ArrayList<>();
            list.forEach(it -> {
                PumpRoomVo entity = new PumpRoomVo();
                entity.setName(it.getExtends1());
                entity.setLongitude(it.getLongitude());
                entity.setLatitude(it.getLatitude());
                entity.setRoomId(it.getExtends2());
                entity.setBuildingName(it.getExtends3());
                entity.setStructureType(it.getExtends4());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                roomList.add(entity);
            });
            resultList = roomList;
        } else if (type == 12) {
            ArrayList<PowerhouseVo> powerhouseList = new ArrayList<>();
            list.forEach(it -> {
                PowerhouseVo entity = new PowerhouseVo();
                entity.setName(it.getExtends1());
                entity.setStructureType(it.getExtends2());
                entity.setPosition(it.getExtends3());
                entity.setLongitude(it.getLongitude());
                entity.setLatitude(it.getLatitude());
                entity.setType(it.getExtends4());
                entity.setConstruction(it.getExtends5());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                powerhouseList.add(entity);
            });
            resultList = powerhouseList;
        } else if (type == 13) {
            ArrayList<BaseStationVo> baseStationList = new ArrayList<>();
            list.forEach(it -> {
                BaseStationVo entity = new BaseStationVo();
                entity.setNumber(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setStructureType(it.getExtends3());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                baseStationList.add(entity);
            });
            resultList = baseStationList;
        } else if (type == 14) {
            ArrayList<GasStoreVo> gasStoreList = new ArrayList<>();
            list.forEach(it -> {
                GasStoreVo entity = new GasStoreVo();
                entity.setNumber(it.getExtends1());
                entity.setName(it.getExtends2());
                entity.setStructureType(it.getExtends3());
                if (inputMethod == 1) {
                    entity.setDisasterRate6State(it.getDisasterRate6State());
                    entity.setDisasterRate6StateCorrect(it.getDisasterRate6StateCorrection());
                    entity.setDisasterRate7State(it.getDisasterRate7State());
                    entity.setDisasterRate7StateCorrect(it.getDisasterRate7StateCorrection());
                    entity.setDisasterRate8State(it.getDisasterRate8State());
                    entity.setDisasterRate8StateCorrect(it.getDisasterRate8StateCorrection());
                    entity.setDisasterRate9State(it.getDisasterRate9State());
                    entity.setDisasterRate9StateCorrect(it.getDisasterRate9StateCorrection());
                    entity.setDisasterRate10State(it.getDisasterRate10State());
                    entity.setDisasterRate10StateCorrect(it.getDisasterRate10StateCorrection());
                } else {
                    entity.setDisasterRateState(it.getDisasterRateState());
                    entity.setDisasterRateStateCorrect(it.getDisasterRateStateCorrection());
                }
                gasStoreList.add(entity);
            });
            resultList = gasStoreList;
        }
        return resultList;
    }

    @Override
    public VectorVo getVectorByVersionId(String versionId, Integer type) {
        SmxVectorEntity entity = estimateRepository.getVectorByVersionId(versionId, type);
        if (PlatformObjectUtils.isEmpty(entity)) {
            return null;
        }
        return VectorVo.builder()
                .id(entity.getId())
                .province(entity.getProvince())
                .city(entity.getCity())
                .county(entity.getCounty())
                .versionCode(entity.getVersionCode())
                .fileName(entity.getFileName())
                .createUserName(entity.getCreateUserName())
                .build();
    }

    @Override
    public void exportAllExcel(String taskId, HttpServletResponse response) throws Exception {
        LifelineTaskEntity entity = taskManagementRepository.getEntityById(taskId);
        Integer inputMethod = entity.getInputMethod();

        Map<Integer, String> typeSheetNames = new HashMap<>();
        typeSheetNames.put(1, "地下管网");
        typeSheetNames.put(2, "水厂水池");
        typeSheetNames.put(3, "燃气-储气罐");
        typeSheetNames.put(4, "桥梁");
        typeSheetNames.put(5, "道路");
        typeSheetNames.put(6, "变电站");
        typeSheetNames.put(8, "电缆线路");
        typeSheetNames.put(9, "通信室内设备");
        typeSheetNames.put(11, "水厂泵房");
        typeSheetNames.put(12, "重要发电厂房");
        typeSheetNames.put(13, "通信基站");
        typeSheetNames.put(14, "燃气-门站-场站");
        typeSheetNames.put(15, "供气管网");

        Workbook workbook = new XSSFWorkbook();

        for (Map.Entry<Integer, String> entry : typeSheetNames.entrySet()) {
            Integer type = entry.getKey();
            String sheetName = entry.getValue();
            List<EvaluationResults> list = estimateRepository.getListByTaskIdAndType(taskId, type);
            List<EvaluationResults> lineList = estimateRepository.getLineListByTaskIdAndType(taskId, type);
            list.addAll(lineList);
            Object dataList = dataProcessing(type, list, inputMethod);
            if (type == 1 || type == 15) {
                if (inputMethod == 1) {
                    List<UpNetworkVo> upNetworkList = (List<UpNetworkVo>) dataList;
                    addSheetToWorkbook(workbook, upNetworkList, sheetName, UpNetworkVo.class);
                } else {
                    List<UpNetworkIfVo> upNetworkList = (List<UpNetworkIfVo>) dataList;
                    addSheetToWorkbook(workbook, upNetworkList, sheetName, UpNetworkIfVo.class);
                }
            } else if (type == 2) {
                if (inputMethod == 1) {
                    List<PoolVo> poolVoList = (List<PoolVo>) dataList;
                    addSheetToWorkbook(workbook, poolVoList, sheetName, PoolVo.class);
                } else {
                    List<PoolIfVo> poolVoList = (List<PoolIfVo>) dataList;
                    addSheetToWorkbook(workbook, poolVoList, sheetName, PoolIfVo.class);
                }
            } else if (type == 3) {
                if (inputMethod == 1) {
                    List<GasVo> gasVoList = (List<GasVo>) dataList;
                    addSheetToWorkbook(workbook, gasVoList, sheetName, GasVo.class);
                } else {
                    List<GasIfVo> gasVoList = (List<GasIfVo>) dataList;
                    addSheetToWorkbook(workbook, gasVoList, sheetName, GasIfVo.class);
                }
            } else if (type == 4) {
                if (inputMethod == 1) {
                    List<BridgeVo> bridgeList = (List<BridgeVo>) dataList;
                    addSheetToWorkbook(workbook, bridgeList, sheetName, BridgeVo.class);
                } else {
                    List<BridgeIfVo> bridgeList = (List<BridgeIfVo>) dataList;
                    addSheetToWorkbook(workbook, bridgeList, sheetName, BridgeIfVo.class);
                }
            } else if (type == 5) {
                if (inputMethod == 1) {
                    List<RoadVo> roadList = (List<RoadVo>) dataList;
                    addSheetToWorkbook(workbook, roadList, sheetName, RoadVo.class);
                } else {
                    List<RoadIfVo> roadList = (List<RoadIfVo>) dataList;
                    addSheetToWorkbook(workbook, roadList, sheetName, RoadIfVo.class);
                }
            } else if (type == 6) {
                if (inputMethod == 1) {
                    List<TransformerSubstationVo> tsList = (List<TransformerSubstationVo>) dataList;
                    addSheetToWorkbook(workbook, tsList, sheetName, TransformerSubstationVo.class);
                } else {
                    List<TransformerSubstationIfVo> tsList = (List<TransformerSubstationIfVo>) dataList;
                    addSheetToWorkbook(workbook, tsList, sheetName, TransformerSubstationIfVo.class);
                }
            } else if (type == 8) {
                if (inputMethod == 1) {
                    List<CablingVo> cablingList = (List<CablingVo>) dataList;
                    addSheetToWorkbook(workbook, cablingList, sheetName, CablingVo.class);
                } else {
                    List<CablingIfVo> cablingList = (List<CablingIfVo>) dataList;
                    addSheetToWorkbook(workbook, cablingList, sheetName, CablingIfVo.class);
                }
            } else if (type == 12) {
                if (inputMethod == 1) {
                    List<PowerhouseVo> powerhouseList = (List<PowerhouseVo>) dataList;
                    addSheetToWorkbook(workbook, powerhouseList, sheetName, PowerhouseVo.class);
                } else {
                    List<PowerhouseIfVo> powerhouseList = (List<PowerhouseIfVo>) dataList;
                    addSheetToWorkbook(workbook, powerhouseList, sheetName, PowerhouseIfVo.class);
                }
            } else if (type == 13) {
                if (inputMethod == 1) {
                    List<BaseStationVo> baseStationList = (List<BaseStationVo>) dataList;
                    addSheetToWorkbook(workbook, baseStationList, sheetName, BaseStationVo.class);
                } else {
                    List<BaseStationIfVo> baseStationList = (List<BaseStationIfVo>) dataList;
                    addSheetToWorkbook(workbook, baseStationList, sheetName, BaseStationIfVo.class);
                }
            } else if (type == 14) {
                if (inputMethod == 1) {
                    List<GasStoreVo> gasStoreList = (List<GasStoreVo>) dataList;
                    addSheetToWorkbook(workbook, gasStoreList, sheetName, GasStoreVo.class);
                } else {
                    List<GasStoreIfVo> gasStoreList = (List<GasStoreIfVo>) dataList;
                    addSheetToWorkbook(workbook, gasStoreList, sheetName, GasStoreIfVo.class);
                }
            }
        }
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("生命线工程评估结果" + ".xlsx", "UTF-8"));
        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
        } finally {
            workbook.close();
        }
    }

    private <T> void addSheetToWorkbook(Workbook workbook, List<T> dataList, String sheetName, Class<T> clazz) throws Exception {
        Sheet sheet = workbook.createSheet(sheetName);
        Map<String, String> headerMapping = getHeaderMappingFromAnnotation(clazz);
        Row headerRow = sheet.createRow(0);
        int colIndex = 0;
        for (String fieldName : headerMapping.keySet()) {
            Cell cell = headerRow.createCell(colIndex++);
            cell.setCellValue(headerMapping.get(fieldName));
        }
        int rowIndex = 1;
        for (T data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            colIndex = 0;
            for (String fieldName : headerMapping.keySet()) {
                Cell cell = row.createCell(colIndex++);
                Object value = getFieldValue(data, fieldName);
                cell.setCellValue(value == null ? "" : value.toString());
            }
        }
    }

    private Object getFieldValue(Object obj, String fieldName) throws Exception {
        Field field = obj.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(obj);
    }

    private static Map<String, String> getHeaderMappingFromAnnotation(Class<?> clazz) {
        List<Field> fields = Arrays.asList(clazz.getDeclaredFields());
        List<Field> sortedFields = new ArrayList<>(fields);

        // 按 orderNum 排序字段
        sortedFields.sort((f1, f2) -> {
            Excel excel1 = f1.getAnnotation(Excel.class);
            Excel excel2 = f2.getAnnotation(Excel.class);
            int order1 = excel1 == null ? Integer.MAX_VALUE : Integer.parseInt(excel1.orderNum());
            int order2 = excel2 == null ? Integer.MAX_VALUE : Integer.parseInt(excel2.orderNum());
            return Integer.compare(order1, order2);
        });

        Map<String, String> headerMapping = new LinkedHashMap<>();
        for (Field field : sortedFields) {
            if (field.isAnnotationPresent(Excel.class)) {
                Excel excel = field.getAnnotation(Excel.class);
                headerMapping.put(field.getName(), excel.name()); // 字段名与中文名映射
            }
        }
        return headerMapping;
    }
}
